//
//  JobTrainingViewController.m
//  jinneng
//
//  Created by mac on 2020/8/5.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "JobTrainingViewController.h"
#import "TrainingDetailViewController.h"
#import "TrainingCodeViewController.h"
#import "TrainingSignViewController.h"
#import "TrainingConsulotionViewController.h"
@interface JobTrainingViewController ()
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@end

@implementation JobTrainingViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"安全培训";
    
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
    self.tabBarController.tabBar.hidden = YES;
    [self initView];
    [self registerFunction];
}

-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    [self.jznWebView.scrollView.mj_header beginRefreshing];
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.bottom.equalTo(self.view);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/safe_training" type:@"html"];
    
    [self.jznWebView.scrollView.mj_header setBackgroundColor:[UIColor whiteColor]];
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestLearn:self.page];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
    
    [self registerFunction];
}
-(void) registerFunction{
    
    [self.webViewBridge registerHandler:@"toSign" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString* trainingClassId = data[@"trainingClassId"];
        dispatch_async(dispatch_get_main_queue(), ^(){
            TrainingSignViewController *vc = [[TrainingSignViewController alloc]init];
            [vc setTrainingClassId:trainingClassId];
            [self.navigationController pushViewController:vc animated:YES];
       });
    }];
    
    [self.webViewBridge registerHandler:@"toCode" handler:^(id data, WVJBResponseCallback responseCallback) {
          NSString *trainingClassName = data[@"trainingClassName"];
          NSString *trainingClassId = data[@"trainingClassId"];
           dispatch_async(dispatch_get_main_queue(), ^(){
               TrainingCodeViewController *vc = [[TrainingCodeViewController alloc]init];
                 [vc setTrainingClassId:trainingClassId];
                 [vc setTitleString:@"培训签到"];
                 [vc setSignType:@"0"];
                 [vc setTrainingClassName:trainingClassName];
               [self.navigationController pushViewController:vc animated:YES];
           });
       }];
    
    [self.webViewBridge registerHandler:@"toDetailController" handler:^(id data, WVJBResponseCallback responseCallback) {
             NSString *trainingInfo = [data mj_JSONString];
             dispatch_async(dispatch_get_main_queue(), ^(){
                 TrainingDetailViewController *vc = [[TrainingDetailViewController alloc]init];
                 vc.trainingInfo = trainingInfo;
                 [self.navigationController pushViewController:vc animated:YES];
             });
      }];
    
    [self.webViewBridge registerHandler:@"toConsulotion" handler:^(id data, WVJBResponseCallback responseCallback) {
             NSString* trainingClassId = data[@"trainingClassId"];
             dispatch_async(dispatch_get_main_queue(), ^(){
                 TrainingConsulotionViewController *vc = [[TrainingConsulotionViewController alloc]init];
                 vc.trainingClassId = trainingClassId;
                 [self.navigationController pushViewController:vc animated:YES];
             });
      }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
     
}

-(void) requestLearn:(int) page{
    NSString *url = [NSString stringWithFormat:@"%@/trainingClass/findStudentTrainingClass" ,[URLUtil hostName]];
    NSDictionary *params = @{@"trainingType":[NSString stringWithFormat:@"%d",1],@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId],@"pageNum":[NSString stringWithFormat:@"%d",page],@"pageSize":@"10"};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            if (page == 1) {
                NSString *clear = @"clear()";
               // [self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
                [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                    
                }];
                [self.jznWebView.scrollView.mj_header endRefreshing];
                [self.jznWebView.scrollView.mj_footer resetNoMoreData];
            }
            
            NSString *clear = [NSString stringWithFormat:@"getTrainingList(%@)",[dataResult.data mj_JSONString]];
            //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
            NSArray *array = dataResult.data;
            if([array count]<10){
                
                [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
            }else{
                
                [self.jznWebView.scrollView.mj_footer endRefreshing];
            }
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}


@end
